//
//  UIColor+Extention.m
//  YunHaiManage
//
//  Created by zhangbolin on 2021/12/17.
//

#import "UIColor+Extention.h"

@implementation UIColor (Extention)

/// 返回一个 UIColor 对象
/// @param red 红色色值 (0x00-0xff)
/// @param green 绿色色值 (0x00-0xff)
/// @param blue 蓝色色值 (0x00-0xff)
/// @param alpha 不透明的程度 (0-1)
UIColor *QJNRGBA(HexColorType red, HexColorType green, HexColorType blue, CGFloat alpha){
    return [UIColor colorWithRed:(CGFloat)red/255.0f green:(CGFloat)green/255.0f blue:(CGFloat)blue/255.0f alpha:alpha];
}

/// 返回一个 UIColor 对象
/// @param red 红色色值 (0x00-0xff)
/// @param green 绿色色值 (0x00-0xff)
/// @param blue 蓝色色值 (0x00-0xff)
UIColor *QJNRGB(HexColorType red, HexColorType green, HexColorType blue){
    return [UIColor colorWithRed:(CGFloat)red/255.0f green:(CGFloat)green/255.0f blue:(CGFloat)blue/255.0f alpha:1];
}

/**
 * 从一个Int型的数字中提取颜色值
 *
 * 比如说:
 *
 * 0xff9900   表示 red 为0xff， green 为 0x99，blue 为 0x00， 透明程度为 0x00（取值范围0x00-0xff，越大越透明）
 * 0x80ff9900 表示 red 为0xff， green 为 0x99，blue 为 0x00， 透明程度为 0x80（取值范围0x00-0xff，越大越透明）
 *
 * @param rgbUInt 一个由 int 数表示的颜色值，argb，a表示透明程度；32位，小端
 * @return UIColor
 */
UIColor *QJNIRGB(NSUInteger rgbUInt){
    HexColorType a, r, g, b;

    a = 0xff - (((rgbUInt >> 24) & 0xff));
    r = ((rgbUInt >> 16) & 0xff);
    g = ((rgbUInt >> 8) & 0xff);
    b = (rgbUInt & 0xff);
    
    return QJNRGBA(r, g, b, a/255.0);
}

/**
 * 从一个字符串中提取颜色值，没有透明度
 *
 * @param color 可为：0xd5d5d5、#d5d5d5、d5d5d5
 * @return UIColor
 * @author dandan.chen
 * @date 2015/10/22
 */
UIColor *QJNColorWithHexString(NSString *color)
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]){
        cString = [cString substringFromIndex:2];
    }
    
    if ([cString hasPrefix:@"#"]){
        cString = [cString substringFromIndex:1];
    }
    
    if ([cString length] != 6){
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return QJNRGBA(r, g, b, 1);
}

#pragma mark - 新方法
/// RGB color 生成UIColor对象
/// @param rgbValue 例如0xff4e3c
+ (UIColor *)qjn_colorWithRGB:(NSInteger)rgbValue {
    return [UIColor qjn_colorWithRGB:rgbValue alpha:1];
}

/// RGB color + 透明度 生成UIColor对象
/// @param rgbValue 例如0xff4e3c
/// @param alpha 0~1
+ (UIColor *)qjn_colorWithRGB:(NSInteger)rgbValue alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0f
                           green:((rgbValue & 0xFF00) >> 8) / 255.0f
                            blue:(rgbValue & 0xFF) / 255.0f
                           alpha:alpha];
}

/// 使用HEX命名方式的颜色字符串生成一个UIColor对象
/// @param hexString  例如0xd5d5d5、#d5d5d5、d5d5d5
+ (UIColor *)qjn_colorWithHexString:(NSString *)hexString {
    return [self qjn_colorWithHexString:hexString alpha:1.0];
}

/// 使用HEX命名方式的颜色字符串生成一个UIColor对象
/// @param hexString 例如0xd5d5d5、#d5d5d5、d5d5d5
/// @param alpha 透明度 0~1
+ (UIColor *)qjn_colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha {
    NSString *tranString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([hexString containsString:@"grouptableviewbackgroundcolor"]) {
        return [UIColor groupTableViewBackgroundColor];
    } else if ([hexString containsString:@"black"]) {
        return [UIColor blackColor];
    } else if ([hexString containsString:@"darkgray"]) {
        return [UIColor darkGrayColor];
    } else if ([hexString containsString:@"lightgray"]) {
        return [UIColor lightGrayColor];
    } else if ([hexString containsString:@"white"]) {
        return [UIColor whiteColor];
    } else if ([hexString containsString:@"gray"]) {
        return [UIColor grayColor];
    } else if ([hexString containsString:@"red"]) {
        return [UIColor redColor];
    } else if ([hexString containsString:@"green"]) {
        return [UIColor greenColor];
    } else if ([hexString containsString:@"blue"]) {
        return [UIColor blueColor];
    } else if ([hexString containsString:@"cyan"]) {
        return [UIColor cyanColor];
    } else if ([hexString containsString:@"yellow"]) {
        return [UIColor yellowColor];
    } else if ([hexString containsString:@"magenta"]) {
        return [UIColor magentaColor];
    } else if ([hexString containsString:@"orange"]) {
        return [UIColor orangeColor];
    } else if ([hexString containsString:@"purple"]) {
        return [UIColor purpleColor];
    } else if ([hexString containsString:@"brown"]) {
        return [UIColor brownColor];
    } else if ([hexString containsString:@"clear"]) {
        return [UIColor clearColor];
    }
    
    // String should be 6 or 8 characters
    if ([tranString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([tranString hasPrefix:@"0X"]){
        tranString = [tranString substringFromIndex:2];
    }
    
    if ([tranString hasPrefix:@"#"]){
        tranString = [tranString substringFromIndex:1];
    }
    
    if ([tranString length] != 6){
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [tranString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [tranString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [tranString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r/255.f) green:((float) g/255.f) blue:((float) b/255.f) alpha:alpha];
}

@end
